<template>
    <div class="container" v-if="readyShow">

        <el-steps :active="active" align-center v-if="showAgent">
            <!--   <el-step :title="identityName"></el-step>-->
            <el-step title="代理人"></el-step>
        </el-steps>

        <el-steps :active="active" align-center v-if="creditor">
            <el-step :title="identityName"></el-step>
        </el-steps>

        <el-steps :active="active" align-center v-if="agentType === 2">
            <el-step title="特别申报"></el-step>
        </el-steps>

        <div style="margin-top: 8%"></div>
        <register-creditor v-show="showCreditor" :form="formCreditor" :especially="especially"
                           :creditorButton="creditorButton"
                           @nextPage="nextPageCreditor"
                           v-if="identityType == 1"></register-creditor>
        <register-organization v-show="showOrganization" :form="formCreditor" :creditorButton="creditorButton"
                               @nextPage="nextPageCreditor"
                               v-if="identityType == 2"></register-organization>
        <register-government v-show="showGovernment" :form="formCreditor" :creditorButton="creditorButton"
                             @nextPage="nextPageCreditor"
                             v-if="identityType == 3"></register-government>
        <register-agent v-show="showAgent" :form="formAgent" :showThreeType="showThreeType" :showTwoType="showTwoType"
                        :agentButton="agentButton" @nextPage="nextPageAgent"
                        v-if="agentTypeBoolean"></register-agent>

        <el-dialog
                title="提示"
                :visible.sync="registerCreditorVisible"
                width="90%">
            <span>是否确认提交</span>
            <span slot="footer" class="dialog-footer">
                 <el-button @click="registerCreditorVisible = false" style="margin-right: 5%">取 消</el-button>
                 <el-button type="primary" @click="onSubmit" style="margin-left: 5%">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import RegisterCreditor from 'components/register/register-creditor/register-creditor.vue'
    import RegisterOrganization from 'components/register/register-creditor/register-organization.vue'
    import RegisterGovernment from 'components/register/register-creditor/register-government.vue'
    import RegisterAgent from 'components/register/register-agent.vue'
    import RegisterAccount from 'components/register/register-account.vue'
    import util from 'assets/js/util.js'

    export default {
        data() {
            return {
                showTwoType: true,
                showThreeType: false,
                especially: false,
                readyShow: false,
                creditor: false,
                agent: false,
                active: 0,

                showGovernment: false,
                showAgent: false,
                showCreditor: false,
                showOrganization: false,
                pageCreditor: false,

                pageAgent: false,
                pageAccount: false,
                identityType: 1,
                natureBoolean: false,
                agentType: 0,
                identityName: '',
                agentTypeBoolean: false,
                registerCreditorVisible: false,
                formCreditor: {
                    fileList: [],
                    identityType: '',
                    orgName: '',
                    orgCode: '',
                    name: '',
                    idType: 1,
                    idcard: '',
                    mobile: '',
                    email: '',
                    attendState: '不确定',
                    address: '',
                    tel: '',
                    state: '',
                    specialCreditor: '',
                    specialNote: '',
                    especiallyFileList: [],
                },
                formAgent: {
                    name: '',
                    idcardFront: "-1",
                    idcardReverse: "-1",
                    idcardFrontVisible: false,
                    idcardReverseVisible: false,
                    idType: 1,
                    idcard: '',
                    mobile: '',
                    email: '',
                    address: '',
                    fileList: [],
                },
                formAccount: {
                    bankName: '',
                    bankUname: '',
                    bankAccount: ''
                },
                agentButton: '提交',
                creditorButton: '提交',
                caseId: 0,
                creditorId: 0
            }
        },
        mounted() {
            this.identityType = parseInt(util.getUrlParam(document.location.href, 'identityType'));
            this.agentType = parseInt(util.getUrlParam(document.location.href, 'agentType'));

            const _self = this;
            util.ajax({
                url: `/creditor/detail`,
                params: {},
                success: function (data) {
                    _self.formCreditor.orgName = data.data.orgName || '';
                    _self.formCreditor.identityType = _self.identityType;   //债权人身份(1：个人，2：公司(机构)，3：行政机关（金融机构）
                    _self.identityName = _self.identityNameText(_self.formCreditor.identityType);
                    _self.formCreditor.orgCode = data.data.orgCode || '';
                    _self.formCreditor.orgFileIds = data.data.orgFileIds || '';
                    _self.formCreditor.name = data.data.name || '';
                    _self.formCreditor.idType = _self.matchTypeText(data.data.idType || 1);
                    //_self.formCreditor.idType = data.data.idType || 1;
                    _self.formCreditor.idcard = data.data.idcard || '';
                    _self.formCreditor.mobile = data.data.mobile || '';
                    _self.formCreditor.email = data.data.email || '';
                    _self.formCreditor.address = data.data.address || '';
                    _self.formCreditor.tel = data.data.tel || '';
                    _self.formCreditor.idcardFront = data.data.idcardFront || "-1";
                    _self.formCreditor.idcardReverse = data.data.idcardReverse || "-1";
                    _self.formCreditor.bankName = data.data.bankName || '';
                    _self.formCreditor.bankUname = data.data.bankUname || '';
                    _self.formCreditor.bankAccount = data.data.bankAccount || '';
                    _self.formCreditor.attendState = _self.attendStateText(data.data.attendState || 0);

                    _self.formCreditor.specialCreditor = data.data.specialCreditor || '';
                    _self.formCreditor.specialNote = data.data.specialNote || '';
                    _self.formCreditor.specialFileIds = data.data.specialFileIds || '';
                    _self.formCreditor.state = data.data.state;
                    _self.creditor = true;
                    //代理人类型
                    if (_self.identityType === 2) {
                        _self.showOrganization = true
                    }
                    if (_self.identityType === 3) {
                        _self.showGovernment = true
                    } else {
                        _self.showCreditor = true
                    }

                    if (_self.agentType === 2) {
                        _self.creditor = false;
                        _self.especially = true;
                    }
                    // 特别申报 ： 2       agentType:0：本人，1：代理人  2：特别申报,
                    // 代理人 ： 1
                    if (_self.agentType === 1) {
                        _self.formAgent.name = data.data.agentDetailInfo.name || '';
                        _self.formAgent.identityType = data.data.agentDetailInfo.identityType; //1、操作委托人2、律师委托3、公司负责人
                        _self.formAgent.idcardFront = data.data.agentDetailInfo.idcardFront || "-1";
                        _self.formAgent.idcardReverse = data.data.agentDetailInfo.idcardReverse || "-1";
                        _self.formAgent.idType = data.data.agentDetailInfo.idType || 1;
                        _self.formAgent.idcard = data.data.agentDetailInfo.idcard || '';
                        _self.formAgent.mobile = data.data.agentDetailInfo.mobile || '';
                        _self.formAgent.email = data.data.agentDetailInfo.email || '';
                        _self.formAgent.address = data.data.agentDetailInfo.address || '';
                        _self.formAgent.agentDetailFileids = data.data.agentDetailInfo.agentDetailFileids || '';

                        if (_self.identityType === 1) {
                            _self.showTwoType = true
                        } else {
                            _self.showTwoType = false;
                            _self.showThreeType = true
                        }
                        _self.showOrganization = false;
                        _self.showGovernment = false;
                        _self.showCreditor = false;

                        _self.creditor = false;
                        _self.showAgent = true;
                        _self.agentTypeBoolean = true;

                    }
                    _self.readyShow = true;
                },
            });

        },
        methods: {
            identityNameText: function (type) {
                let typeText = '';
                switch (type) {
                    case 1:
                        typeText = '个人';
                        break;
                    case 2 :
                        typeText = '公司(机构)';
                        break;
                    case 3 :
                        typeText = '行政机关（金融机构）';
                        break;
                    default:
                        break;
                }
                return typeText
            },

            attendStateText: function (type) {
                let typeText = '不确定';
                switch (type) {
                    case 0:
                        typeText = '不确定';
                        break;
                    case  1:
                        typeText = '参加现场会议';
                        break;
                    case 2 :
                        typeText = '参加在线会议';
                        break;
                    default:
                        break;
                }
                return typeText
            },
            attendStateType: function (type) {
                let typeText = 0;
                switch (type) {
                    case '不确定':
                        typeText = 0;
                        break;
                    case  '参加现场会议':
                        typeText = 1;
                        break;
                    case '参加在线会议' :
                        typeText = 2;
                        break;
                    default:
                        break;
                }
                return typeText
            },
            matchTypeText: function (type) {
                let typeText = '';
                switch (type) {
                    case 1:
                        typeText = '身份证';
                        break;
                    case 2 :
                        typeText = '执业证';
                        break;
                    case 3:
                        typeText = '护照';
                        break;
                    default:
                        break;
                }
                return typeText
            },
            /* matchIdTypeText: function (type) {
                 let typeText = '';
                 switch (type) {
                     case '身份证':
                         typeText = 1;
                         break;
                     case  '执业证':
                         typeText = 2;
                         break;
                     default:
                         break;
                 }
                 return typeText
             },*/

            nextPageCreditor: function (formData) {
                this.formCreditor = formData;
                this.onSubmit();
            },
            nextPageAgent: function (formData) {
                this.formAgent = formData;
                //this.onSubmitAgent();
                this.onSubmit();
            },
            /*previousPageAgent: function (formData) {
                this.formAgent = formData;
                //this.nextPage(false)
            },*/
            /*nextPage: function (nextOrPrevious) {
                this.initialPage()
                if (nextOrPrevious) {
                    if (this.active++ > 1)
                        this.active = 0
                }
                else {
                    if (this.active-- < 0)
                        this.active = 0
                }

                switch (this.active) {
                    case 0:
                        this.pageCreditor = true;
                        break;
                    case 1:
                        this.pageAgent = true;
                        break;
                        break;
                }
            },*/
            /* initialPage: function () {
                 this.pageCreditor = false;
                 this.pageAgent = false
             },*/
            onSubmit: function () {
                if (!this.registerCreditorVisible) {
                    this.registerCreditorVisible = true;
                    return;
                }
                let form = new FormData();
                form.append('identityType', this.identityType);
                form.append('agentType', this.agentType);
                if (!this.agentTypeBoolean) {
                    if (this.formCreditor.idcardFront !== '-1')
                        form.append('idcardFront', this.formCreditor.idcardFront);
                    if (this.formCreditor.idcardReverse !== '-1')
                        form.append('idcardReverse', this.formCreditor.idcardReverse);
                    if (this.formCreditor.name !== '')
                        form.append('name', this.formCreditor.name);
                    if (this.formCreditor.idType !== 3)
                        form.append('idType', this.formCreditor.idType);
                    if (this.formCreditor.idcard !== '')
                        form.append('idcard', this.formCreditor.idcard);
                    if (this.formCreditor.mobile !== '')
                        form.append('mobile', this.formCreditor.mobile);
                    if (this.formCreditor.tel !== '')
                        form.append('tel', this.formCreditor.tel);
                    if (this.formCreditor.email !== '')
                        form.append('email', this.formCreditor.email);
                    form.append('address', this.formCreditor.address);
                    if (this.formCreditor.attendState !== '')
                        form.append('attendState', this.attendStateType(this.formCreditor.attendState));
                    if (this.formCreditor.orgName !== '')
                        form.append('orgName', this.formCreditor.orgName);
                    if (this.formCreditor.orgCode !== '')
                        form.append('orgCode', this.formCreditor.orgCode);
                    if (this.formCreditor.orgFileIds !== '')
                        form.append('orgFileIds', this.formCreditor.orgFileIds);

                    if (this.formCreditor.bankName !== '')
                        form.append('bankName', this.formCreditor.bankName);
                    if (this.formCreditor.bankUname !== '')
                        form.append('bankUname', this.formCreditor.bankUname);
                    if (this.formCreditor.bankAccount !== '')
                        form.append('bankAccount', this.formCreditor.bankAccount);

                    if (this.identityType === 1 && this.agentType === 2) {
                        if (this.formCreditor.specialCreditor !== '')
                            form.append('specialCreditor', this.formCreditor.specialCreditor);
                        if (this.formCreditor.specialNote !== '')
                            form.append('specialNote', this.formCreditor.specialNote);
                        if (this.formCreditor.specialFileIds !== '')
                            form.append('specialFileIds', this.formCreditor.specialFileIds);
                    }
                }

                if (this.agentTypeBoolean) {
                    form.append('idcardFront', this.formAgent.idcardFront);
                    if (this.formAgent.idcardReverse !== '-1')
                        form.append('idcardReverse', this.formAgent.idcardReverse);
                    form.append('name', this.formAgent.name);
                    form.append('address', this.formAgent.address);
                    form.append('identityType', this.formAgent.identityType);
                    form.append('idType', this.formAgent.idType);
                    form.append('idcard', this.formAgent.idcard);
                    form.append('mobile', this.formAgent.mobile);
                    if (this.formAgent.email !== '') {
                        form.append('email', this.formAgent.email);
                    }
                    form.append('agentDetailFileids', this.formAgent.agentDetailFileids);
                    console.log(this.formAgent)
                }

                const _self = this;
                if (this.agentTypeBoolean) {
                    util.ajaxByForm({
                        url: `/creditor/updateCreditorAgent`,
                        form: form,
                        success: function (data) {
                            alert('修改代理人成功!');
                            window.location.href = '../creditor/creditor.html?';
                            _self.registerCreditorVisible = false;
                        },
                        error: function (error) {
                            alert(error);
                        },
                    });
                } else {
                    util.ajaxByForm({
                        url: `/creditor/updateCreditor`,
                        form: form,
                        success: function (data) {
                            alert('修改债权人成功!');
                            window.location.href = '../creditor/creditor.html?';
                            _self.registerCreditorVisible = false;
                        },
                        error: function (error) {
                            alert(error);
                        },

                    });
                }


                /*  util.ajaxByForm({
                      url: `/creditor/updateCreditorInfo`,
                      form: form,
                      success: function (data) {
                          if (data.data === true) {
                              alert('修改债权人成功!');
                              window.location.href = '../index/index.html?';
                          } else {
                              alert(data.error);
                          }
                          _self.registerCreditorVisible = false;
                      },
                      error: function (error) {
                          alert(error.error);
                      }
                  });*/
            }
        },
        components: {
            RegisterCreditor,
            RegisterOrganization,
            RegisterGovernment,
            RegisterAgent,
            RegisterAccount
        }
    }
</script>

<style>
    .container {
        margin-top: 5%;
    }

    .el-step__main div {
        font-size: 10px;
    }
</style>
